package pm.oo;

import com.sun.star.beans.PropertyValue;
import com.sun.star.frame.XFrame;
import com.sun.star.lang.XComponent;
import static pm.oo.util.DocumentUtil.*;
import static pm.oo.util.Executor.dispatch;
import pm.oo.util.OfficeConnection;
import static pm.oo.util.FramesUtil.createViewFrame;
import pm.core.PrintProperties;
import pm.core.CopyProperties;

import java.io.File;
import java.io.IOException;

public class OOModuleImpl implements OOModule {
	private XComponent currentDocument;

	private XFrame viewFrame;
	private PropertyValue[] loadProperties;

	public OOModuleImpl(final Long hwnd, final String ooExecFolder) throws OOException {
		try {
			OfficeConnection.createConnection(ooExecFolder);
			viewFrame = createViewFrame(hwnd);
			loadProperties = setupLoadProps();
		} catch (Exception e) {
			throw new OOException(e);
		}
	}

	public void load(final File file) throws OOException {
		try {
			String url = "file:///" + file.getCanonicalPath().replace('\\', '/');
			currentDocument = loadDocument(viewFrame, url, loadProperties);
		} catch (IOException e) {
			throw new OOException(e);
		}
	}

	public void closeCurrentDocument() {
		closeDocument(currentDocument);
	}

	public void nextPage() {
		dispatch(viewFrame, ".uno:PageUp", null);
	}

	public void prevPage() {
		dispatch(viewFrame, ".uno:PageDown", null);
	}

	public void print(PrintProperties printProperties) {
		//todo
	}

	public void printFile(File file) {
		//To change body of implemented methods use File | Settings | File Templates.
	}

	public void print(CopyProperties copyProperties) {
		//To change body of implemented methods use File | Settings | File Templates.
	}

	public void disconnect() {
		OfficeConnection.disconnect();
	}
}
